package cn.com.byte1024.controller;

import cn.com.byte1024.entity.Vote;
import cn.com.byte1024.entity.VoteProject;
import cn.com.byte1024.service.VoteProjectService;
import cn.com.byte1024.service.VoteService;
import cn.com.byte1024.utils.CommonUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: VoteSystem
 * @ClassName: VoteController
 * @Description:
 * @Author: Kwnda
 * @Date: 2021/1/6 2:01
 */
@Controller
@RequestMapping("/vote")
public class VoteController {

    @Autowired
    private VoteService voteService;

    @Autowired
    private VoteProjectService voteProjectService;

    /**
     * 
     * @param voteProjectId
     * @param model
     * @return
     */
    @RequestMapping("/goVoteJsp")
    public String goVoteJsp(@RequestParam("voteProjectId") String voteProjectId, Model model) {
        getVoteList(voteProjectId, model);
        return "vote";
    }

    /**
     * 
     * @param voteProjectId
     * @param model
     */
    private void getVoteList(String voteProjectId, Model model) {
        List<Vote> voteList = voteService.selectAllVoteByVoteProjectId(voteProjectId);
        model.addAttribute("voteList", voteList);
        int num = voteList.size();
        model.addAttribute("num", num);
        model.addAttribute("voteProjectId", voteProjectId);
    }

    /**
     * 
     * @param voteProjectId
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(@RequestParam("voteProjectId") String voteProjectId, Model model) {
        model.addAttribute("voteProjectId", voteProjectId);
        return "vote-add";
    }

    /**
     * 
     * @param voteProjectId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/openAdd")
    public String openAdd(@RequestParam("voteProjectId") String voteProjectId, HttpServletRequest request) {
        //判断该项目是否是请求用户创建的，如果不是允许打开
        String userId = request.getSession().getAttribute("userId").toString();

        VoteProject voteProject = voteProjectService.selectByPrimaryKey(voteProjectId);
        //判断该项目是否是请求用户创建的，如果不是允许打开
        if (userId.equals(voteProject.getUserId())) {
            //是,打开窗口
            return "success";
        } else {
            //不是允许打开
            return "error";
        }
    }

    /**
     * 
     * @param voteImg
     * @param voteProjectId
     * @param voteTitle
     * @param voteContext
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("/voteSave")
    public String voteSave(MultipartFile voteImg, @RequestParam("voteProjectId") String voteProjectId, @RequestParam("voteTitle") String voteTitle, @RequestParam("voteContext") String voteContext, Model model) throws IOException {
        
        String filePath = "C:\\Users\\11986\\Desktop\\VoteSystem\\src\\main\\webapp\\img";//保存图片的路径

        String originalFilename = voteImg.getOriginalFilename();

        //新的文件名字
        String newFileName = CommonUtil.generateUUID()+originalFilename;

        //封装上传文件位置的全路径
        File targetFile = new File(filePath,newFileName);

        //把本地文件上传到封装上传文件位置的全路径
        voteImg.transferTo(targetFile);

        // 数据库新增操作 存到实体类
        Vote vote = new Vote();
        vote.setVoteId(CommonUtil.generateUUID());

        vote.setVoteTitle(voteTitle);
        
        vote.setVoteImg("../img/"+newFileName);

        vote.setVoteContext(voteContext);

        vote.setVoteTicket(0);
        
        // 新增操作,在VoteProjectVote表添加数据
        voteService.insertSelective(vote, voteProjectId);

        getVoteList(voteProjectId, model);
        return "vote";
    }

    /**
     * 
     * @param voteImg
     * @param voteId
     * @param voteProjectId
     * @param voteTitle
     * @param voteContext
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("/voteUpdate")
    public String voteUpdate(MultipartFile voteImg,@RequestParam("voteId") String voteId, @RequestParam("voteProjectId") String voteProjectId, @RequestParam("voteTitle") String voteTitle, @RequestParam("voteContext") String voteContext, Model model) throws IOException {
        
        String filePath = "C:\\Users\\11986\\Desktop\\VoteSystem\\src\\main\\webapp\\img";//保存图片的路径

        String originalFilename = voteImg.getOriginalFilename();

        //新的文件名字
        String newFileName = CommonUtil.generateUUID()+originalFilename;

        //封装上传文件位置的全路径
        File targetFile = new File(filePath,newFileName);

        //把本地文件上传到封装上传文件位置的全路径
        voteImg.transferTo(targetFile);

        // 数据库新增操作 存到实体类
        Vote vote = new Vote();
        vote.setVoteId(voteId);

        vote.setVoteTitle(voteTitle);

        vote.setVoteImg("../img/"+newFileName);

        vote.setVoteContext(voteContext);

        // 新增操作,在VoteProjectVote表添加数据
        voteService.updateByPrimaryKeySelective(vote);

        getVoteList(voteProjectId, model);
        return "vote";
    }

    /**
     * 
     * @param voteProjectId
     * @param voteId
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public String delete(@RequestParam("voteProjectId") String voteProjectId,@RequestParam("voteId") String voteId,HttpServletRequest request,Model model){
        String userId=request.getSession().getAttribute("userId").toString();
        boolean flag = voteService.deleteVote(voteProjectId,voteId,userId);
        if(flag){
            getVoteList(voteProjectId, model);
            return "success";   
        }else{
            return "error";
        }
    }

    /**
     * 
     * @param voteProjectId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/openEdit")
    public String openEdit(@RequestParam("voteProjectId") String voteProjectId,HttpServletRequest request){
        String userId = request.getSession().getAttribute("userId").toString();
        VoteProject voteProject = voteProjectService.selectByPrimaryKey(voteProjectId);
        if(userId.equals(voteProject.getUserId())){
            return "success";
        }else{
            return "error";
        }
    }

    /**
     * 
     * @param voteId
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("voteId") String voteId,Model model){
        Vote vote = voteService.selectByPrimaryKey(voteId);
        model.addAttribute("vote",vote);
        return "vote-edit";
    }

    /**
     * 点击投票按钮增加一票
     * @param voteProjectId
     * @param voteId
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/voteAdd",produces="text/html;charset=UTF-8")
    public String voteAdd(@RequestParam("voteProjectId") String voteProjectId,@RequestParam("voteId") String voteId,HttpServletRequest request,Model model){
        String userId = request.getSession().getAttribute("userId").toString();
        String result=voteService.voteAdd(voteProjectId,voteId,userId);
        return result;
    }
}
